package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.ICardDao;
import com.qymerge.manage.base.dao.IReportItemDao;
import com.qymerge.manage.base.ro.ReportItemRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.constant.ParamKey;
import com.qymerge.manage.core.dao.BaseDao;
import com.qymerge.manage.core.utils.DateUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Title: ReportItemDaoImpl</p>
 * <p>Description: 报表项目</p>
 *
 * @author Xiao
 * @date 2025/4/28 13:36
 * @version 1.0.0
 */
@Repository
public class ReportItemDaoImpl extends BaseDao implements IReportItemDao {

    private String dwd_MCRS_MDM_report_item = "SELECT id, name, create_user_id, create_time, status, sort FROM dwd_MCRS_MDM_report_item WHERE 1=1 ";

    @Override
    public List list(ReportItemRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_report_item);
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getId() > 0) {
            sql.append("and id=:id ");
            paramMap.put("id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql.append("and name like :name ");
            paramMap.put("name", "%" + ro.getName() + "%");
        }

        if (StringUtils.isNotBlank(ro.getCreate_time_begin()) && StringUtils.isNotBlank(ro.getCreate_time_end())) {
            sql.append(" and creaet_time BETWEEN :create_time_begin AND :create_time_end");
            paramMap.put("create_time_begin", DateUtil.getLongTimeByYYYYMMDDHHmm(ro.getCreate_time_begin()));
            paramMap.put("create_time_end", DateUtil.getLongTimeByYYYYMMDDHHmm(ro.getCreate_time_end()));
        }

        // 分页数据获取
        sql.append(" order by sort desc limit :skip,:sizes");
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql.toString(), paramMap, new BeanPropertyRowMapper<ReportItemRO>(ReportItemRO.class));
    }

    @Override
    public int total(ReportItemRO ro) {
        StringBuffer sql = new StringBuffer("select count(1) from dwd_MCRS_MDM_report_item where 1=1 ");
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getId() > 0) {
            sql.append("and id=:id ");
            paramMap.put("id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql.append("and name like :name ");
            paramMap.put("name", "%" + ro.getName() + "%");
        }

        return getNamedParameterJdbcTemplate().queryForObject(sql.toString(), paramMap, Integer.class);
    }

    @Override
    public ReportItemRO findOne(Long id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_report_item);
        sql.append("and id=?");
        RowMapper<ReportItemRO> rowMapper = new BeanPropertyRowMapper<ReportItemRO>(ReportItemRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<ReportItemRO> findAll(ReportItemRO ro) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_report_item);
        Map<String, Object> paramMap = new HashMap<String, Object>();

        if (ro.getId() > 0) {
            sql.append(" and id=:id");
            paramMap.put("id", ro.getId());
        }

        if (StringUtils.isNotBlank(ro.getName())) {
            sql.append(" and name like :name");
            paramMap.put("name", "%" + ro.getName() + "%");
        }

        sql.append(" order by id desc");
        return getNamedParameterJdbcTemplate().query(sql.toString(), paramMap, new BeanPropertyRowMapper<ReportItemRO>(ReportItemRO.class));
    }

    @Override
    public int install(ReportItemRO ro) {
        StringBuilder sql = new StringBuilder("insert into dwd_MCRS_MDM_report_item(name, status, sort, create_user_id, create_time) values(?,?,?,?,?)");

        List params = new ArrayList();
        params.add(ro.getName());
        params.add(ro.getStatus());
        params.add(ro.getSort());
        params.add(ro.getCreate_user_id());
        params.add(ro.getCreate_time());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int update(ReportItemRO ro) {
        StringBuilder sql = new StringBuilder("update dwd_MCRS_MDM_report_item set name=:name, status=:status, sort=:sort, update_user_id=:update_user_id, update_time=:update_time where id=:id ");

        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("name", ro.getName());
        paramsMap.put("status", ro.getStatus());
        paramsMap.put("sort", ro.getSort());
        paramsMap.put("update_user_id", ro.getUpdate_user_id());
        paramsMap.put("update_time", ro.getUpdate_time());
        paramsMap.put("id", ro.getId());
        return getNamedParameterJdbcTemplate().update(sql.toString(), paramsMap);
    }

    @Override
    public int remove(List<Long> ids) {
        // 删除
        String sql="delete from dwd_MCRS_MDM_report_item where id=?";
        final List<Long> idss = ids;
        BatchPreparedStatementSetter setterMid = new BatchPreparedStatementSetter(){

            @Override
            public int getBatchSize(){
                return idss.size();
            }

            @Override
            public void setValues(PreparedStatement ps, int i){
                Long id = idss.get(i);
                try {
                    ps.setLong(1, id);
                } catch(Exception e){
                    logger.error("batchInsertWaitAward Exception:", e);
                }
            }
        };
        return getJdbcTemplate().batchUpdate(sql, setterMid).length;
    }


}
